/**
 * SpringMVC restful 风格
 * 用户控制器，控制关于用户的一些业务逻辑操作
 * @author yanwl
 * @date 2014-12-09
 */
package com.chnbst.mbstyun.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chnbst.mbstyun.pojo.MessageCheck;
import com.chnbst.mbstyun.pojo.User;
import com.chnbst.mbstyun.service.MessageCheckService;
import com.chnbst.mbstyun.service.UserService;
import com.chnbst.mbstyun.utils.CreateRandom;
import com.chnbst.sms.SMSUtil;
import com.chnbst.sms.enumeration.EncodingType;

/**
 * 控制器注解 请求映射注解 controller 代表类是控制器 restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * 
 * @author yanwl
 * @date 2014-12-09
 */

@Controller
@RequestMapping("/user")
public class UserController {
	/**
	 * 注入 业务逻辑层
	 */
	@Resource
	private UserService userService;
	@Resource
	private MessageCheckService messageCheckService;
	@Resource
	private UserOperator userOperator;
	
	/**
	 * post 请求 执行查询用户
	 * @param userTel
	 * @return 布尔值 true:用户不存在 false:用户存在
	 * @throws Exception
	 */
	@RequestMapping(value = "/tel", method = RequestMethod.POST)
	@ResponseBody
	public boolean selectUserByTel(String userTel) throws Exception {
		boolean success = true;
		User user = userService.TelExists(userTel);
		if (user != null) {
			success = false;
		}
		return success;
	}
	
	/**
	 * 用户注册获取短信验证码
	 * @param tel
	 * @return 布尔值 true:发送成功 false:发送失败
	 */
	@RequestMapping(value = "/getMsgCode", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getMsgCode(String tel, HttpServletRequest request)
			throws Exception {
		String message = null;
		String getMsgCode = CreateRandom.createRandom(true, 6);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date currentDate = new Date();
		Map<String, Object> map = new HashMap<String, Object>();
		MessageCheck messageCheck = messageCheckService.selectByToTel(tel);
		if (messageCheck == null) {
			String success = SMSUtil.sendMessage(new String[] { tel },
					"MB-2014090144", new String[] { getMsgCode },
					EncodingType.UTF8, null);
			if (success.indexOf("发送成功") != -1) {
				request.getSession().setAttribute("messageCode", getMsgCode);
				MessageCheck messageCheck2 = new MessageCheck();
				messageCheck2.setToTel(tel);
				messageCheck2.setSendTime(currentDate);
				messageCheck2.setSendNum(1);
				messageCheckService.insertMessage(messageCheck2);
				message = "发送成功";
			} else {
				message = "发送失败";
			}
		} else {
			Integer sendNum = messageCheck.getSendNum();
			Date senDate = messageCheck.getSendTime();
			String date = format.format(senDate);
			String currentTime = format.format(currentDate);
			if (sendNum < 3) {
				if (date.equals(currentTime)) {
					String success = SMSUtil.sendMessage(new String[] { tel },
							"MB-2014090144", new String[] { getMsgCode },
							EncodingType.UTF8, null);
					if (success.indexOf("发送成功") != -1) {
						request.getSession().setAttribute("messageCode",
								getMsgCode);
						messageCheck.setSendNum(messageCheck.getSendNum() + 1);
						messageCheckService.updateMessageCheck(messageCheck);
						message = "发送成功";
					} else {
						message = "发送失败";
					}
				} else {
					String success = SMSUtil.sendMessage(new String[] { tel },
							"MB-2014090144", new String[] { getMsgCode },
							EncodingType.UTF8, null);
					if (success.indexOf("发送成功") != -1) {
						request.getSession().setAttribute("messageCode",
								getMsgCode);
						messageCheck.setSendTime(currentDate);
						messageCheck.setSendNum(1);
						messageCheckService.updateMessageCheck(messageCheck);
						message = "发送成功";
					} else {
						message = "发送失败";
					}

				}

			} else {
				if (!date.equals(currentTime)) {
					String success = SMSUtil.sendMessage(new String[] { tel },
							"MB-2014090144", new String[] { getMsgCode },
							EncodingType.UTF8, null);
					if (success.indexOf("发送成功") != -1) {
						request.getSession().setAttribute("messageCode",
								getMsgCode);
						messageCheck.setSendTime(currentDate);
						messageCheck.setSendNum(1);
						messageCheckService.updateMessageCheck(messageCheck);
						message = "发送成功";
					} else {
						message = "发送失败";
					}

				} else {
					message = "每天最多只能发送三次";
				}
			}
		}
		map.put("message", message);
		return map;
	}
}
